<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/21
 * Time: 13:44
 */

namespace app\admin\controller;

use app\common\model\User as UserModel;
use app\common\model\UserAccountRecord;

class User extends CheckLogin
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $page = (int)$this->request->post('pageIndex');
            $limit = (int)$this->request->post('pageSize');
            $name = $this->request->post('dataName', '');
            $offset = ($page - 1) * $limit;
            $where = [];
            empty($name) or $where['nickname'] = ['like', '%' . $name . '%'];
            $userModel = new UserModel();
            $totalCount = $userModel->where($where)->count();
            $list = $totalCount > 0 ? $userModel->allUser($where, $offset, $limit) : [];
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    $v->account;
                }
            }
            $data = [
                'code' => 0,
                'msg' => 'success',
                'list' => $list,
                'count' => $totalCount
            ];

            return $this->json($data);
        } else {
            return $this->fetch();
        }
    }

    public function record()
    {
        if ($this->request->isAjax()) {
            $page = (int)$this->request->post('pageIndex');
            $limit = (int)$this->request->post('pageSize');
            $userId = $this->request->post('dataUser', 0);
            $offset = ($page - 1) * $limit;
            $where = [];
            empty($userId) or $where['user_id'] = $userId;
            $recordModel = new UserAccountRecord();
            $totalCount = $recordModel->where($where)->count();
            $list = $totalCount > 0 ? $recordModel->allAccountRecord($where, $offset, $limit) : [];
            if (!empty($list)) {
                $type = UserAccountRecord::$_type;
                foreach ($list as $k => $v) {
                    $list[$k]['type_format'] = $type[$v['type']];
                }
            }
            $data = [
                'code' => 0,
                'msg' => 'success',
                'list' => $list,
                'count' => $totalCount
            ];

            return $this->json($data);
        } else {
            $userId = $this->request->param('user_id');
            $user = UserModel::all();
            $this->assign('user', $user);
            $this->assign('user_id', $userId);
            return $this->fetch();
        }
    }
}

